package com.mou.controller.user;

import com.mou.entity.EmailRequest;
import com.mou.result.Result;
import com.mou.service.CaptchaService;
import com.mou.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Email;

// AuthController.java
@RestController
@Slf4j
@RequestMapping("/user/captcha")
public class CaptchaController {
    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private EmailService emailService;
    
    // 发送验证码接口
    @PostMapping("/sendCaptcha")
    public Result<String> sendCaptcha(@RequestBody @Email EmailRequest request) {
        String email = request.getEmail();
        log.info("发送验证码: {}", email);
        if (!email.endsWith("@qq.com")) {
            return Result.error("仅支持QQ邮箱注册");
        }
        
        String code = captchaService.generateCode(email);
        emailService.sendCaptchaEmail(email, code);
        return Result.success("验证码已发送");
    }
}

